<template>
  <div class="app-container">
    <el-card class="filter-container" shadow="never">
    <div>
      <i class="el-icon-search"></i>
      <span>筛选搜索</span>
      <el-button
        style="float:right"
        type="primary"
        @click="handleSearchList()"
        size="small"
      >
        查询搜索
      </el-button>
      <el-button
        style="float:right;margin-right: 15px"
        @click="handleResetSearch()"
        size="small"
      >
        重置
      </el-button>
    </div>
    <div style="margin-top: 15px">
      <el-form :inline="true" :model="listQuery" size="small" label-width="140px">
        <el-form-item label="输入搜索：">
          <el-input v-model="listQuery.email" class="input-width" placeholder="Email"></el-input>
        </el-form-item>
      </el-form>
    </div>
    </el-card>
    <el-card class="operate-container" shadow="never">
      <i class="el-icon-tickets"></i>
      <span>数据列表</span>
    </el-card>
    <div class="table-container">
      <el-table ref="orderTable"
                :data="list"
                style="width: 100%;"
                v-loading="listLoading" border
      >
        <el-table-column type="selection" width="60" align="center"></el-table-column>
        <el-table-column label="编号" width="80" align="center">
          <template slot-scope="scope">{{ scope.row.id }}</template>
        </el-table-column>
        <el-table-column label="用户名" width="180" align="center">
          <template slot-scope="scope">{{ scope.row.name }}</template>
        </el-table-column>
        <el-table-column label="Email" width="180" align="center">
          <template slot-scope="scope">{{ scope.row.email }}</template>
        </el-table-column>
        <el-table-column label="最后登录时间" width="180" align="center">
          <template slot-scope="scope">{{ scope.row.last_login_time | formatTime }}</template>
        </el-table-column>
        <el-table-column label="用户账号" align="center">
          <template slot-scope="scope">{{ scope.row.nickname }}</template>
        </el-table-column>
        <el-table-column label="积分" width="120" align="center">
          <template slot-scope="scope">$ {{ scope.row.points }}</template>
        </el-table-column>
        <el-table-column label="操作" width="200" align="center">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="success"
              @click="openAddPointDialog(scope.row.id)"
            >充值积分
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="pagination-container">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        layout="total, sizes,prev, pager, next,jumper"
        :current-page.sync="listQuery.page"
        :page-size="listQuery.limit"
        :page-sizes="[20,30,50]"
        :total="total"
      >
      </el-pagination>
    </div>

    <el-dialog
      title="充值积分"
      :visible.sync="dialogVisible" width="30%"
    >
      <span style="vertical-align: top">积分：</span>
      <el-input-number
        style="width: 80%"
        v-model="addPoints.points"
      >
      </el-input-number>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitAddPoints">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import { fetchList, addPoints } from '@/api/user/user'

import { parseTime } from '@/utils'

const defaultListQuery = {
  page: 1,
  size: 20,
  email: null,
}

const defaultAddPoints= {
  user_id: 0,
  points: 0,
}

export default {
  name: 'User',
  components: {},
  data() {
    return {
      listQuery: Object.assign({}, defaultListQuery),
      listLoading: true,
      list: null,
      total: 0,
      dialogVisible: false,
      addPoints: Object.assign({}, defaultAddPoints)
    }
  },
  created() {
    this.getList()
  },
  filters: {
    formatTime(time) {
      return parseTime(time)
    },
  },
  methods: {
    handleResetSearch() {
      this.listQuery = Object.assign({}, defaultListQuery)
    },
    handleSearchList() {
      this.listQuery.pageNum = 1
      this.getList()
    },
    getList() {
      this.listLoading = true
      fetchList(this.listQuery).then(response => {
        this.listLoading = false
        this.list = response.data
        this.total = response.total
      })
    },
    handleSizeChange(val) {
      this.listQuery.page = 1
      this.listQuery.size = val
      this.getList()
    },
    handleCurrentChange(val) {
      this.listQuery.page = val
      this.getList()
    },
    openAddPointDialog(user_id){
      this.dialogVisible = true
      this.addPoints.user_id = user_id
    },
    submitAddPoints() {
      this.$confirm('是否要添加积分操作?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        addPoints(this.addPoints).then(response => {
          this.$message({
            message: '添加成功！',
            type: 'success',
            duration: 1000
          })
          this.dialogVisible = false
          this.addPoints = defaultAddPoints
          this.getList()
        })
      })
    }
  }
}
</script>
